package com.letoken.platform.repo.app.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.admin.req.mall.MallIssuDetailReq;
import com.letoken.platform.repo.admin.req.mall.MallStoreAccountListReq;
import com.letoken.platform.repo.admin.req.user.FundRankingListBonusRecordReq;
import com.letoken.platform.repo.admin.res.mall.MallIssuDetailRes;
import com.letoken.platform.repo.admin.res.mall.MallStoreAccountListRes;
import com.letoken.platform.repo.app.po.AppUserAssetsHistory;
import com.letoken.platform.repo.app.res.assets.QueryTokenAssetsHistoryRes;
import com.letoken.platform.repo.helper.CalculateConsumerValue;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * Le账户-资产历史交易日志 Mapper 接口
 * </p>
 *
 * @author 作者
 * @since 2022-12-10
 */
public interface AppUserAssetsHistoryMapper extends BaseMapper<AppUserAssetsHistory> {

    Page<QueryTokenAssetsHistoryRes> queryTokenAssetsHistory(@Param("page")
                                                             Page<QueryTokenAssetsHistoryRes> page,
                                                             @Param("tokenId") String tokenId,
                                                             @Param("split") List<String> split,
                                                             @Param("userId") Integer userId);

//    Page<QueryLetokenHistoryRes> queryLetokenHistoryPage(@Param("page") Page<QueryLetokenHistoryRes> page,
//                                                         @Param("userId") Integer userId);


    Page<MallStoreAccountListRes> storeAccountList(@Param("page")  Page<MallStoreAccountListRes> page,@Param("req") MallStoreAccountListReq req);

    @Select("select ifnull(SUM(amount),0) from app_user_assets_history where type in (9) and DATE(create_time) = CURDATE()")
    BigDecimal getAmountByToday();

    @Select("select IFNULL(SUM(amount),0)from app_user_assets_history where type in (9) and to_days(now( ) ) - to_days( create_time) = 1  ")
    BigDecimal getAmountByYesterday();

    @Select("select IFNULL(SUM(amount),0) from app_user_assets_history where type in (9) and MONTH(create_time) = MONTH(NOW()) and create_time >= '2023-10-12 15:00:00'")
    BigDecimal getAmountByMonth();
    @Select("select IFNULL(SUM(amount),0) from app_user_assets_history where type in (9)  and create_time >= '2023-10-12 15:00:00'")
    BigDecimal getSumAmountBy();

    Page<MallIssuDetailRes> getMallIssueDetail(@Param("page")  Page<MallIssuDetailRes> page,@Param("req") MallIssuDetailReq req);

    @Select("select * from app_user_assets_history where user_id=#{userId} and sn = #{sn} limit 1")
    AppUserAssetsHistory getAppUserAssetsHistory(@Param("userId")Integer userId,@Param("sn")String sn);


    Page<AppUserAssetsHistory> pageList(@Param("page") Page<AppUserAssetsHistory> page ,@Param("userId") Integer userId,@Param("tokenId") Integer tokenId);

    Page<AppUserAssetsHistory> pageByType(@Param("page") Page<AppUserAssetsHistory> page ,@Param("params") FundRankingListBonusRecordReq params);


    @Select("SELECT user_id ,ROUND(SUM(amount),8) as amount from app_user_assets_history where  create_time > #{date} and  `type` in (16,20,21,22,27)  group by user_id ")
    List<CalculateConsumerValue.ObtainAssetPo> obtainAssetAmount(@Param("date") String date);


    @Select("SELECT ROUND(SUM(amount),8) from app_user_assets_history where user_id = #{userId} and  create_time > #{date} and   `type` in (16,20,21,22,27) ")
    BigDecimal obtainAssetAmountUser(@Param("userId") Integer userId,@Param("date") String date);



//    BigDecimal obtainAssetAmountUser(@Param("userId") Integer userId,@Param("date") String date);

    @Select("select IFNULL(SUM(amount),0) from app_user_assets_history where   date(create_time) = #{date} and type = #{type}")
    BigDecimal sumAmountByDateAndType(@Param("date") LocalDate date, @Param("type") Integer type);


    @Select("select IFNULL(SUM(amount),0) from app_user_assets_history where  type = #{type}")
    BigDecimal sumAmountByType(@Param("type") Integer type);

    @Select("select IFNULL(SUM(amount),0) from app_user_assets_history where   create_time >= #{startDateTime} and create_time <= #{endDateTime} and type = #{type}")
    BigDecimal sumAmountByDateTimeAndType(@Param("startDateTime") LocalDateTime startDateTime,@Param("endDateTime") LocalDateTime endDateTime, @Param("type") Integer type);


    BigDecimal sumAmountByTypeAndUserIdsAndDate(@Param("type") Integer type,@Param("userIds") List<Integer> userId, @Param("startDatetime") LocalDateTime date, @Param("endDatetime") LocalDateTime endDatetime);


}
